home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 201 / 201.xpi / components / debugService.js < prev    next >
Text File  |  2010-01-11  |  4KB  |  160 lines

  1. /* You may find the license in the LICENSE file */
  2.  
  3. function include(uri) {
  4.     Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  5.         .getService(Components.interfaces.mozIJSSubScriptLoader)
  6.         .loadSubScript(uri);
  7. }
  8. include('chrome://dta/content/common/xpcom.jsm');
  9.  
  10. const FileStream = new Components.Constructor('@mozilla.org/network/file-output-stream;1', 'nsIFileOutputStream', 'init');
  11. const ScriptError = new Components.Constructor('@mozilla.org/scripterror;1', 'nsIScriptError', 'init');
  12.  
  13. var DebugService = {
  14.     // nsIObserver
  15.     observe: function DS_observe(subject, topic, prefName) {
  16.         this._setEnabled(this._pb.getBoolPref('extensions.dta.logging'));    
  17.     },
  18.     
  19.     init: function DS_init() {
  20.         this._cs = Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService);
  21.         this._pb = Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefBranch2);
  22.         this._pb.addObserver('extensions.dta.logging', this, true);
  23.         this._setEnabled(this._pb.getBoolPref('extensions.dta.logging'));
  24.         
  25.         this._file = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("ProfD", Ci.nsILocalFile);
  26.         this._file.append('dta_log.txt');
  27.         
  28.         try {
  29.             if (this._file.fileSize > (200 * 1024)) {
  30.                 this.remove();
  31.             }
  32.         }
  33.         catch(ex) {
  34.             // No-Op
  35.         }
  36.         delete this.init;
  37.     },
  38.     get file() {
  39.         return this._file;
  40.     },
  41.     get enabled() {
  42.         return this._enabled;
  43.     },
  44.     _setEnabled: function DS_setEnabled(nv) {
  45.         this._enabled = nv;
  46.         if (nv) {
  47.             this.logString = this.log = this._log;
  48.         }
  49.         else {
  50.             this.logString = this.log = this._logDisabled;
  51.         }
  52.     },
  53.     _formatTimeDate: function DS_formatTimeDate(value) {
  54.         return String(value).replace(/\b(\d)\b/g, "0$1");
  55.     },
  56.     _log: function DS__log(msg, exception) {
  57.         try {
  58.             if (!msg || (msg == "" && typeof(exception) != "object")) {
  59.                 return;
  60.             }
  61.             if (!(msg instanceof String) && typeof(msg) != 'string') {
  62.                 for (var i = 0; i < 10 && msg.wrappedJSObject; ++i) {
  63.                     msg = msg.wrappedJSObject;
  64.                 }
  65.                 msg = msg.toSource();
  66.             }
  67.             let time = new Date();
  68.             let text = this._formatTimeDate(time.getHours())
  69.                 + ":" + this._formatTimeDate(time.getMinutes())
  70.                 + ":" + this._formatTimeDate(time.getSeconds())
  71.                 + ":" + time.getMilliseconds()
  72.                 + "\n";
  73.  
  74.             if (msg != "") {
  75.                 text += msg.replace(/\n/g, "\n\t") + " ";
  76.             }
  77.             if (exception) {
  78.                 text += "\tError: " + exception;
  79.             }
  80.             text += "\r\n";
  81.             let stack = null;
  82.             let lineNumber = 0;
  83.             let fileName = null;
  84.             if (Components.stack) {
  85.                 stack = Components.stack.caller.caller;
  86.             }
  87.             if (exception && exception.stack) {
  88.                 lineNumber = exception.lineNumber;
  89.                 fileName = exception.fileName;
  90.                 let initialLine = "Frame :: " + fileName;
  91.                 if (exception.location) {
  92.                     initialLine += " :: " + exception.location;
  93.                 }
  94.                 else if (stack && stack.name) {
  95.                     initialLine += " :: " + stack.name;
  96.                 }
  97.                 initialLine += " :: line: " + lineNumber;
  98.                 text += "\t> " + initialLine + "\n";
  99.             }
  100.             else if (stack) {
  101.                 text += "\t> " + stack.toString() + "\n";
  102.                 lineNumber = stack.lineNumber;
  103.                 fileName = stack.fileName;
  104.                 
  105.             }
  106.             
  107.             if (stack) {
  108.                 let s = stack.caller;
  109.                 for (let i = 0; i < 4 && s; ++i) {
  110.                     text += "\t> " + s.toString() + "\n";
  111.                     s = s.caller;
  112.                 }
  113.                 if (stack && exception) {
  114.                     this._cs.logMessage(new ScriptError(text, fileName, null, lineNumber, 0, 0x2, 'component javascript'));
  115.                      
  116.                 } 
  117.                 else {
  118.                     this._cs.logStringMessage(text);
  119.                 }
  120.             }
  121.             else {
  122.                 this._cs.logStringMessage(text);
  123.             }
  124.             
  125.             var f = new FileStream(this.file, 0x04 | 0x08 | 0x10, 0664, 0);
  126.             f.write(text, text.length);
  127.             f.close();
  128.         }
  129.         catch(ex) {
  130.             error(ex);
  131.         }    
  132.     
  133.     },
  134.     _logDisabled: function DS__dumpDisabled() {
  135.         // no-op;
  136.     },
  137.     log: this._log,
  138.     logString: this._log,
  139.         
  140.     remove: function DS_remove() {
  141.         try {
  142.             this._file.remove(false);
  143.         }
  144.         catch (ex) {
  145.             throw Cr.NS_ERROR_FAILURE;
  146.         }
  147.     }
  148. };
  149. implementComponent(
  150.     DebugService,
  151.     Components.ID("{0B82FEBB-59A1-41d7-B31D-D5A686E11A69}"),
  152.     "@downthemall.net/debug-service;1",
  153.     "DownThemAll! Debug Service",
  154.     [Ci.nsIObserver, Ci.dtaIDebugService]
  155. );
  156.  
  157. // entrypoint
  158. function NSGetModule(compMgr, fileSpec) {
  159.     return new ServiceModule(DebugService, false);
  160. }